<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="盘点编号" prop="inventory_id">
        <el-input v-model="queryParams.inventory_id" placeholder="请输入盘点编号" clearable size="small" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column type="selection" align="center" width="50" />
      <el-table-column prop="inventory_id" label="盘点编号" align="center" />
      <el-table-column prop="inventory" label="盘点方式" align="center" />
      <el-table-column prop="config_detail" label="盘点属性" align="center" />
      <el-table-column prop="number" label="数量" align="center" />
      <el-table-column prop="box_number" label="箱数" align="center" />
      <el-table-column prop="created_at" label="创建时间" align="center" width="150" />
      <el-table-column prop="disc_surplus" label="总盘盈" align="center" />
      <el-table-column prop="disc_deficit" label="总盘亏" align="center" />
      <el-table-column prop="total_number" label="总实盘数量" align="center" />
      <el-table-column prop="accuracy_rate" label="正确率" align="center" />
      <el-table-column prop="user_name" label="盘点人" align="center" />
      <el-table-column prop="status" label="盘点状态" align="center">
        <template v-slot="scope">
          <span v-if="scope.row.status === '0'">盘点中</span>
          <span v-else-if="scope.row.status === '1'">完结</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="detailed(scope.row)">盘点明细
          </el-button>
          <el-button v-if="scope.row.status === '0'" size="mini" type="danger" @click="handleDel(scope.row)">完结盘点
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page"
      :limit.sync="queryParams.limit"
      @pagination="getList"
    />
    <!-- 新增/修改 -->
    <AddEdit :open.sync="open" :title="title" :form="form" @getList="getList" />
    <!-- 盘点明细-->
    <Detailed :open.sync="open1" :title="title1" :inventory_id="inventory_id" :status="status" @getList="getList" />
  </div>
</template>
<script>
import { inventoryList, inventoryDelete, inventorySonList } from '@/api/stock'
import AddEdit from './AddEdit'
import Detailed from './Detailed'
export default {
  name: 'StockInventory',
  components: {
    AddEdit,
    Detailed
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      open: false,
      open1: false,
      title: '',
      title1: '',
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      form: {},
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10,
        inventory_id: undefined
      },
      show: false,
      ids: [],
      inventory_id: '',
      status: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      const that = this
      this.loading = true
      inventoryList(this.queryParams).then(response => {
        console.log(response)
        if (response.code === 10000) {
          that.list = response.data
          that.list.forEach(item => {
            if (item.inventory_type == '0') {
              item.inventory = '库名/库位'
              item.config_detail = item.config_name
            } else if (item.inventory_type == '1') {
              item.inventory = '物料型号'
              item.config_detail = item.is_all_product === 0 ? '全部商品' : '部分商品'
            } else if (item.inventory_type == '2') {
              item.inventory = '动碰盘点'
              item.config_detail = item.start_datetime + ' - ' + item.end_datetime
            }
          })
          that.total = response.count
          that.loading = false
        }
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form = {}
      this.open = true
      this.title = '添加'
    },
    /** 明细 */
    detailed(row) {
      const queryParams = {
        page: 1,
        limit: 10
      }
      queryParams['inventory_id'] = row.inventory_id
      inventorySonList(queryParams).then(res => {
        if (res.data.length < 1) {
          this.$message.warning('当前盘点单无盘点明细')
          return
        } else {
          this.status = row.status
          this.inventory_id = row.inventory_id
          this.open1 = true
          this.title1 = '盘点明细'
        }
      })
    },
    /** 完结按钮操作 */
    handleDel(row) {
      const that = this
      that.$confirm('完结之后不可更改,确认完结吗？？？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        that.loading = true
        inventoryDelete({ id: row.id.toString() }).then(response => {
          that.loading = false
          if (response.code === 10000) {
            that.$message.success(response.message)
            that.getList()
          } else {
            that.$message.error(response.message)
          }
        })
      }).catch(() => {
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        page: 1,
        limit: 10,
        inventory_id: undefined
      }
      this.handleQuery()
    }
  }
}

</script>

